package com.my.lab.util;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.my.lab.R;

/**
 * 适配器父类
 * @author  HuanPei.tan
 *
 */
public class MyAdapter extends BaseAdapter {
	protected Context context;
	protected List<HashMap<String, Object>> listData;
	protected LayoutInflater lInflater;
	public MyAdapter(Context context, List<HashMap<String, Object>> listData){
		this.context=context;
		this.listData=listData;
		this.lInflater=LayoutInflater.from(context);
	}
	/**
	 * 更新适配器数据
	 * @param listData
	 */
	public void updateListData(List<HashMap<String, Object>> listData){
		this.listData=listData;
		this.notifyDataSetChanged();
	}
	/**
	 * 获取适配器数据
	 * @return
	 */
	public List<HashMap<String, Object>> getListData(){
		return this.listData;
	}
	@Override
	public int getCount() {
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	/**
	 * 显示正在加载视图
	 */
	public void addLoadingItem() {
		HashMap<String, Object> loadingItem = new HashMap<String, Object>();
		loadingItem.put("item_type", "1");
		listData.add(loadingItem);
		notifyDataSetChanged();
	}
	/**
	 * 删除正在加载视图
	 */
	public void delLoadingItem() {
		HashMap<String, Object> loadingItem = listData.get(listData
				.size() - 1);
		if (loadingItem.containsKey("item_type")
				&& "1".equals(loadingItem.get("item_type"))) {
			listData.remove(loadingItem);
			notifyDataSetChanged();
		}
	}
	/**
	 * 显示正在加载视图
	 * @param v
	 * @param item
	 * @return
	 */
	protected View showLoadingItem( HashMap<String, Object> item){
		if (item.containsKey("item_type")) {
			View v = lInflater.inflate(R.layout.loading_other, null);
			if ("1".equals(item.get("item_type").toString())) {
				((TextView) v
						.findViewById(R.id.tv_loading_content))
						.setText("正在加载...");
				v.findViewById(R.id.pb_loading).setVisibility(
						View.VISIBLE);
			}
			return v;
		}else{
			return null;
		}
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return null;
	}
	
	
}
